package com.chenxiao.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenxiao.mybatisplus.entity.Order;
import com.chenxiao.mybatisplus.entity.OrderDto;
import com.chenxiao.mybatisplus.entity.Record;
import com.chenxiao.mybatisplus.mapper.OrderMapper;
import com.chenxiao.mybatisplus.service.OrderService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = {"orderCache"})
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
     @Cacheable(key = "methodName")//此处没写value
    //@Cacheable(value="OrderDto", key="methodName")
    @Override
    public List<OrderDto> queryOrderList() {
        List<OrderDto> list = orderMapper.selectJoinList(OrderDto.class,
                new MPJLambdaWrapper<Order>()
                        .selectAll(Order.class)
                        .select(Record::getCity)
                        .select(Record::getUid)
                        .leftJoin(Record.class, Record::getOrderId, Order::getOrderId)
                                .isNotNull(Record::getOrderId));

        // list.forEach(System.out::println);
        return list;
    }

    //@Cacheable(key = "targetClass + methodName +#p0")//此处没写value
    @Override
    public IPage<OrderDto> queryOrderPage(IPage pageSize) {

       // Page<OrderDto> page = new Page<>(pageSize,5);
      return  orderMapper.selectJoinPage(
              pageSize,
                OrderDto.class,
                new MPJLambdaWrapper<Order>()
                        .selectAll(Order.class)
                        .select(Record::getCity)
                        .select(Record::getUid)
                        .leftJoin(Record.class, Record::getOrderId, Order::getOrderId)
                        .isNotNull(Record::getOrderId)
        );
    }

    @Override
    public void insertOrder(Order order) {
        orderMapper.insert(order);
    }
}
